#include "SC_Init.h"	// MCU initialization header file, including all firmware library header files
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
#include "lcd_comm.h"
#include "machine_stat.h"
#include "comm_wifi.h"
//#include "comm_wifi.h"
extern uint8_t blink_flag_250ms;
extern uint8_t blink_flag_500ms;
extern uint8_t blink_flag_1000ms;
LED_UNION led_union;
uint8_t led_number[3]={0};
uint8_t code tab_Breathing_light[LED_BREATH_MAX_DATA]={
	0  , 2  , 4  , 6  ,//4
	8  , 12 , 16 , 20 ,//4
	24 , 29 , 35 , 41 ,//4
	47 , 53 , 57 , 65 , 69 , 73 ,
	77 , 81 , 85 , 89 , 94 , 99 , 104, 109,
	114, 119, 124, 129, 134, 140, 146, 152,
	158, 164, 170, 176, 182, 188, 195, 202,
	209, 216, 223, 230, 237, 244, 251, 255
};
uint8_t code led_number_code[]={LCD_0,LCD_1,LCD_2,LCD_3,LCD_4,LCD_5,LCD_6,LCD_7,LCD_8,LCD_9};
uint8_t code LCD_letter[]={LCD_A,LCD_B,LCD_C,LCD_D,LCD_E,LCD_F,LCD_N,LCD_V,LCD_X,LCD_L,LCD_H,LCD_O,LCD_K,LCD_W,LCD_P,LCD_S,LCD_T};
uint8_t led_max=0,led_mid=0,led_min=0;
uint8_t breathing_flag=0;
uint8_t Send_step=0;
uint8_t code_mode=0;
uint16_t openpower_delay=0;

void display_state(void);

void delay(uint8_t n)
{
	uint8_t i;
	for (i=0; i<n;i++)
	{
		
	}
}
void delay_us1(void)//500K
{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	//for(;timer>0;timer--);
}
// void delay_us(void)
// {
// 	delay_us1();
// }
void AiP33624_start(void)	
{
	SDA_624 = LED_H;
	delay_us1();
	SCL_624 = LED_H;
	delay_us1();
	SDA_624 = LED_L;
	delay_us1();
	SCL_624 = LED_L;
	delay_us1();
}

void AiP33624_stop(void)
{
	SCL_624 = LED_L;
	delay_us1();
	SDA_624 = LED_L;
	delay_us1();
	SCL_624 = LED_H;
	delay_us1();
	SDA_624 = LED_H;
	delay_us1();
}

void AiP33624_write(unsigned char dat)
{
	unsigned char i;
	for(i = 0; i < 8; i++)
	{

		SCL_624 = LED_L;
		delay_us1();
		if(dat & 0x80)
		{
			SDA_624 = LED_H;
		}
		else
		{
			SDA_624 = LED_L;
		}
		  
		delay_us1();
		SCL_624 = LED_H;
		dat <<= 1;
		delay_us1();
	}
}

void write_cmd(unsigned char cmd)				//�?8位指�?
{
	AiP33624_start();					//�?��??
	AiP33624_write(cmd);				//写入命令
	AiP33624_stop();					//结束
}


/**********************************************************
//	*	函数名称：显示数�?传输
//	*	�?		能：逐灯点亮
//	*	�?		数：
//	*	�? �? 值：�?
***********************************************************/

void AiP33624_WriteData1(unsigned char addr1,unsigned char addr2,unsigned char dat)
{
	write_cmd(0x7C);            //GRID1~GRID6有效，RAM地址  固定
	write_cmd(addr1);
	AiP33624_start();					//�?��??
	AiP33624_write(addr2);				//写入地址
	AiP33624_write(dat);			//初�?�化数据
	AiP33624_stop();					//结束
}

/**********************************************************
//	*	函数名称：显示数�?传输
//	*	�?		能：全RAM写数�?
//	*	�?		数：
//	*	�? �? 值：�?
***********************************************************/
void AiP33624_WriteData(unsigned char addr1,unsigned char dat)
{
	unsigned char i;
	write_cmd(0x78);            //GRID1~GRID8有效，RAM地址�?�?
	write_cmd(0x20);
	AiP33624_start();					//�?��??
	AiP33624_write(addr1);				//写入地址
	for(i=0;i<80;i++)
	{
		AiP33624_write(dat);
	}

	AiP33624_stop();					//结束
}
/**********************************************************
//	*	函数名称：显示数�?定点改写
//	*	�?		能：
//	*	�?		数：
//	*	�? �? 值：�?
***********************************************************/
void AiP33624_WriteData4(unsigned char addr1,unsigned char addr2,unsigned char dat)
{
	write_cmd(0x16);						//设置SEG输出恒流大小（ISEG=6.75mA+CURRENTx0.745mA, CURRENT[4:0])  1d
	write_cmd(0x16);
	write_cmd(0x7C);					//GRID1~GRID8有效，固定地�?��??
	AiP33624_start();					//�?��??
	AiP33624_write(addr1);				//写入地址  addr1对应编号2指令
	AiP33624_stop();					//结束
	AiP33624_start();					//�?��??
	AiP33624_write(addr2);				//写入地址  addr2对应编号5指令

	AiP33624_write(dat);				//写入数据
	AiP33624_stop();					//结束
	write_cmd(0x8c);                        // ，显示开
}
/**********************************************************
//	*	函数名称：芯片初始化
//	*	�?		能：初�?�化内部寄存器及显示数据
//	*	�?		数：
//	*	�? �? 值：
***********************************************************/
void AiP33624_init(void)
{
	write_cmd(0x16);						//设置SEG输出恒流大小（ISEG=6.75mA+CURRENTx0.745mA, CURRENT[4:0])  1d
	write_cmd(0x16);
	write_cmd(0x40);
	write_cmd(0x78);						//GRID1~GRID8有效，RAM地址�?�?
	write_cmd(0x84);						//GRID�?描及�?描间隔时间�?�定，显示关
	AiP33624_WriteData(0xc0,0x00);			//VFD=0,RAM6=0初�?�化显示数据，清显示
	write_cmd(0x8c);                        // ，显示开
}
void LCD_SEG_GRID_writeData(uint8_t led_number,uint8_t led_brightness)
{
	AiP33624_WriteData4((0x20|(led_number>>6)),(0xc0|(led_number&0x3f)),led_brightness);
}
//uint8_t breath_flag=0;
static uint8_t breath_number=0,breath_time=0;
void fire_breath_diaplay(uint8_t fire)
{
	if(IsFactoryMode()) { breath_number = LED_BREATH_MAX_DATA_1;  return;}
	if(fire==0x01)
	{
		//static uint8_t num=0;
		if(0==breath_time)
		{
			breath_number+=1;
		}
		else if(1==breath_time)
		{
			breath_number-=1;
		}
		if((breath_time==1)&&(breath_number==0))
		{
			breath_time=0;
		}
		else if((breath_time==0)&&(breath_number==(LED_BREATH_MAX_DATA_1)))
		{
			breath_time=1;
		}
	//POWER_LED_function(breath_number);

	}
	else
	{
		breath_number=0;
		breath_time=0;
	}
}
uint8_t code shiwei_lcd[7]={LED_07,LED_08,LED_12,LED_13,LED_11,LED_09,LED_10};
uint8_t code gewei_lcd[7]={LED_14,LED_15,LED_19,LED_20,LED_18,LED_16,LED_17};
uint8_t code xiaoshu_lcd[7]={LED_22,LED_23,LED_27,LED_28,LED_26,LED_24,LED_25};
uint8_t code fuzhu1[8]={WIFI_LED,ECO_LED,TEMP_C,POINT,BATHTUB,RESERVE1,RESERVE2,RESERVE3};
uint8_t code fuzhu2[8]={CYCLE,BOOSTER,KITCHEN,BATH,HOT_AND_COOL,FIRE1,FIRE2,FIRE3};
void lcd_fuzhu1_display(uint8_t led_code,uint8_t led_light)
{
	uint8_t i=0;
	for(i=0;i<8;i++)
	{
		if((led_code&(1<<i))==0x00)
		{
			LCD_SEG_GRID_writeData(fuzhu1[i],LED_CLOSE);//4
		}
		else 
		{
			LCD_SEG_GRID_writeData(fuzhu1[i],led_light);//4
		}
	}
}
void lcd_fuzhu2_display(uint8_t led_code,uint8_t led_light)
{
	uint8_t i=0;
	if((led_code&(0xe0))==0xe0)
	{
		fire_breath_diaplay(1);
	}
	else
	{
		fire_breath_diaplay(0);
	}
	for(i=0;i<8;i++)
	{
		if(i>4)
		{
			if((led_code&(1<<i))==0x00)
			{
				LCD_SEG_GRID_writeData(fuzhu2[i],LED_CLOSE);//4
				//fire_breath_diaplay(0);
			}
			else 
			{
				//fire_breath_diaplay(1);
				LCD_SEG_GRID_writeData(fuzhu2[i],tab_Breathing_light[breath_number]);//4
			}
		}
		else
		{
			if((led_code&(1<<i))==0x00)
			{
				LCD_SEG_GRID_writeData(fuzhu2[i],LED_CLOSE);//4
			}
			else 
			{
				LCD_SEG_GRID_writeData(fuzhu2[i],led_light);//4
			}
		}
	}
}
void lcd_shiwei_display(uint8_t led_code,uint8_t led_light)
{
	uint8_t i=0;
	for(i=0;i<7;i++)
	{
		if((led_code&(1<<i))==0x00)
		{
			LCD_SEG_GRID_writeData(shiwei_lcd[i],LED_CLOSE);//4
		}
		else 
		{
			LCD_SEG_GRID_writeData(shiwei_lcd[i],led_light);//4
		}
	}
}
void lcd_gewei_display(uint8_t led_code,uint8_t led_light)
{
	uint8_t i=0;
	for(i=0;i<7;i++)
	{
		if((led_code&(1<<i))==0x00)
		{
			LCD_SEG_GRID_writeData(gewei_lcd[i],LED_CLOSE);//4
		}
		else 
		{
			LCD_SEG_GRID_writeData(gewei_lcd[i],led_light);//4
		}
	}
}
void lcd_xiaoshu_display(uint8_t led_code,uint8_t led_light)
{
	uint8_t i=0;
	for(i=0;i<7;i++)
	{
		if((led_code&(1<<i))==0x00)
		{
			LCD_SEG_GRID_writeData(xiaoshu_lcd[i],LED_CLOSE);//4
		}
		else 
		{
			LCD_SEG_GRID_writeData(xiaoshu_lcd[i],led_light);//4
		}
	}
}
void wifi_display(void){
	if(IsSelfCheckMode()) return;
	if(IsFactoryMode()) return;
	if(WifiState == WIFI_STA_IDLE || WifiState == WIFI_STA_DISLINK_WAIT){
		led_union.led_struct.wifiled = 0;
	}else if(WifiState == WIFI_STA_SUCCESS){
		led_union.led_struct.wifiled = 1;
	}else if(WifiState == WIFI_STA_LINK_OK || WifiState == WIFI_STA_ROUTE){
        if(blink_flag_250ms) 
			led_union.led_struct.wifiled = 1;
		else 
			led_union.led_struct.wifiled = 0;	
	}else if(WifiState == WIFI_STA_DISLINK){
			if(blink_flag_1000ms)
				led_union.led_struct.wifiled=1;
			else 
				led_union.led_struct.wifiled=0;	
	}
}
//uint8_t Send_step
void display_send(void)
{
	Send_step++;
	if(Send_step==1)
	{
		lcd_fuzhu1_display(led_union.led_arry[0],LED_OPEN);
	}
	else if(Send_step==2)
	{
		lcd_fuzhu2_display(led_union.led_arry[1],LED_OPEN);
	}
	else if(Send_step==3)
	{
		lcd_shiwei_display(led_number[0],LED_OPEN);
	}
	else if(Send_step==4)
	{
		lcd_gewei_display(led_number[1],LED_OPEN);
	}
	else if(Send_step>=5)
	{
		Send_step=0;
		lcd_xiaoshu_display(led_number[2],LED_OPEN);
	}
	if(LED_BREATH_MAX_DATA<=breath_number)
	{
		breath_number=0;
	}
}
void dispaly_init(void)
{
	SDA_624 = LED_H;
	//delay_us1();
	SCL_624 = LED_H;
	AiP33624_init();
	AiP33624_WriteData(0xc0,0x00);
	AiP33624_WriteData(0xc0,0xFF);
	led_union.led_arry[1]=0x00;
	led_union.led_arry[0]=0x00;
	led_number[0]=0x00;
	led_number[1]=0x00;
	led_number[2]=0x00;
	breath_number=0x00;
}
void display_all(void)
{
	led_union.led_arry[1]=0xff;
	led_union.led_arry[0]=0xff;
	led_number[0]=0xff;
	led_number[1]=0xff;
	led_number[2]=0xff;
	breath_number=LED_BREATH_MAX_DATA_1;
}
void display_clean(void)
{
	led_union.led_arry[1]=0x00;
	led_union.led_arry[0]=0x00;
	led_number[0]=0x00;
	led_number[1]=0x00;
	led_number[2]=0x00;
	breath_number=0x00;
}
void display_Temperature_Clean(void){
	led_number[0]=0x00;
	led_number[1]=0x00; 
    led_number[2]=0x00;	
	led_union.led_struct.temp_c_led=0;
	led_union.led_struct.pointled=0;

    display_state(); 	
}

void display_cricle(uint8_t cricle)
{
	if(cricle==0x31)
	{
		led_union.led_struct.cycleled=1;
	}
	else
	{
		led_union.led_struct.cycleled=0;
	}
}
void display_pressure(uint8_t pressure)
{
	
	if(pressure==0x31)
	{
		led_union.led_struct.booster=1;
	}
	else
	{
		led_union.led_struct.booster=0;
	}
}

void err_display_clean(uint8_t clean_eco_code,uint8_t clean_fire_code,uint8_t cricle,uint8_t pressure,uint8_t mode,uint8_t coolhot)
{
	led_number[0]=0x00;
	led_number[1]=0x00;
	led_number[2]=0x00;
	led_union.led_struct.temp_c_led=0;
	led_union.led_struct.pointled=0;
}

void display_run(uint16_t temp, uint8_t eco_code,uint8_t fire_code,uint8_t cricle,uint8_t pressure,uint8_t mode,uint8_t yugan,uint8_t coolhot)
{
	static uint16_t temp_old=0x00;
	static uint8_t temp0,temp1,temp2;
	if(temp_old!=temp)
	{
		temp_old=temp;
		temp0=temp/100;
		temp1=(temp/10)%10;
		temp2=temp%10;
	}
	led_number[0]=(led_number_code[(temp0)]); 
	led_number[1]=(led_number_code[(temp1)]);
	led_number[2]=(led_number_code[temp2]);	
	led_union.led_struct.temp_c_led=1;
	led_union.led_struct.pointled=1;

	display_state();
}
void display_version(uint8_t temp1, uint8_t temp2,uint8_t eco_code,uint8_t fire_code,uint8_t cricle,uint8_t pressure)
{
	led_number[0]=(led_number_code[temp1]); 
	led_number[1]=(led_number_code[temp2]);
	led_number[2]=0x00;//(led_number_code[0]);	
	led_union.led_struct.temp_c_led=0;	
	led_union.led_struct.pointled=0;
	
	display_state();
}

void display_err_fire(uint8_t err_code,uint8_t err_code_l)
{

	led_number[0]=(led_number_code[err_code]); 
	led_number[1]=(led_number_code[err_code_l]);
	led_number[2]=0x00;//(led_number_code[0]);	
	led_union.led_struct.temp_c_led=0;	
	led_union.led_struct.pointled=0;
	led_union.led_arry[1]=0x00;
	led_union.led_arry[0]=0x00;
	
}

void display_err_action(uint8_t ac_code,uint8_t ac_code_l,uint8_t eco_code,uint8_t fire_code,uint8_t cricle,uint8_t pressure,uint8_t mode,uint8_t yugan,uint8_t coolhot)
{
	led_number[0]=(led_number_code[ac_code]); 
	led_number[1]=(led_number_code[ac_code_l]);
	led_number[2]=0x00;//(led_number_code[0]);	
	led_union.led_struct.temp_c_led=0;	
	led_union.led_struct.pointled=0;
	
	display_state();
}
#ifdef OPEN_POWER_FLAG
void display_reset_openpowercode_index(void){
    code_mode = 0;
    openpower_delay = 0;
}

void display_openpowercode(uint8_t code0,uint8_t code1,uint8_t code2)//suijima
{
	static uint8_t code_old=0xf0,code3=0xff;
	if((code0>99)||(code1>99)||(code2>99))
	{
		code_old=0xf0;
		code3=0xff;
		code_mode=0;
		openpower_delay=0;
	}
	else
	{
		openpower_delay++;
		if((openpower_delay>=100)&&(code_mode==0))
		{
			openpower_delay=0;
			code_mode=code_mode+1;
			
		}	
		else if((openpower_delay>=200)&&(code_mode!=0))
		{
			openpower_delay=0;
			code_mode=code_mode+1;
			if(code_mode>=4)
			{
				//code3=~code_old;
				code_mode=0;
			}
		
		}
		if(code_mode==0)
		{
			code3=~code_old;
		}
		else if(code_mode==1)
		{
			code3=code0;
		}
		else if(code_mode==2)
		{
			code3=code1;
		}
		else if(code_mode==3)
		{
			code3=code2;
		}
		if(code3!=code_old)
		{
			code_old=code3;
			if((code_mode!=0))
			{
				led_number[0]=(led_number_code[code3/10]); 
				led_number[1]=(led_number_code[code3%10]);//
				led_number[2]=0x00;//(led_number_code[0]);
			}
			else if(code_mode==0)
			{
				led_number[0]=0x40; 
				led_number[1]=0x40;//
				led_number[2]=0x00;//(led_number_code[0]);
			}
		}
	}
	led_union.led_struct.temp_c_led=0;
	led_union.led_struct.pointled=0;	
	
	display_state();
}
void display_openpowercode1(uint8_t code0)// �û�����
{
	static uint8_t code_old=0xfe;

	if(code0<=99)
	{
		if(code_old!=code0)
		{
			code_old=code0;
			led_number[0]=(led_number_code[code0/10]); 
			led_number[1]=(led_number_code[code0%10]);//
			led_number[2]=0x00;//(led_number_code[0]);
		}
	}
	else
	{
		code_old=~code0;
	}	

    led_union.led_struct.temp_c_led=0;
	led_union.led_struct.pointled=0;	

	display_state();
}
void display_openpowercode2(void)// --
{
	led_number[0]=0x40; 
	led_number[1]=0x40;//
	led_number[2]=0x00;//(led_number_code[0]);
	// led_union.led_arry[0]=0x00;
	// led_union.led_arry[1]=0x00;
	led_union.led_struct.temp_c_led=0;
	led_union.led_struct.pointled=0;

	display_state();
}

void display_state(void){
	if(heater_info.eco_stat == 0x31)
		led_union.led_struct.ecoled = 1;
    else
		led_union.led_struct.ecoled = 0;
 
	//breath_number
	if(heater_info.fire_stat == 0x01){
		led_union.led_struct.fire1=1;
		led_union.led_struct.fire2=1;
		led_union.led_struct.fire3=1;
	}else {
		led_union.led_struct.fire1=0;
		led_union.led_struct.fire2=0;
		led_union.led_struct.fire3=0;
	}
	
	if(heater_info.super_mode == 0x31)
		led_union.led_struct.booster=1;
    else
		led_union.led_struct.booster=0;

    if(heater_info.power_stat == 0x30 || heater_info.running_mode == RUN_PLAIN){
		led_union.led_struct.kitchen = 0;
		led_union.led_struct.bathled = 0;
    }else if(heater_info.running_mode == RUN_KITCHEN){
		led_union.led_struct.kitchen = 1;
		led_union.led_struct.bathled = 0;
	}else if(heater_info.running_mode == RUN_SHOWER){
		led_union.led_struct.kitchen = 0;
		led_union.led_struct.bathled = 1;
	}else{
	
	}

	if(heater_info.cool_and_hot == 0x31)
		led_union.led_struct.hotandcool = 1;
	else
		led_union.led_struct.hotandcool = 0;

	if(IsBathtubeFinish() || (heater_info.akey_cycle==0x31 && heater_info.bathtub_mode != 0x00)){
		if(blink_flag_500ms)		
			led_union.led_struct.barhtubled = 1;
		else
			led_union.led_struct.barhtubled = 0;
	}else if(heater_info.bathtub_mode != 0x00)
	    led_union.led_struct.barhtubled = 1;
	else
	    led_union.led_struct.barhtubled = 0;	

	if((heater_info.akey_cycle==0x31)&&((heater_info.bathtub_mode != 0x00))) {
		if(blink_flag_500ms)
			led_union.led_struct.cycleled = 1;
		else
			led_union.led_struct.cycleled = 0;
	} else if(heater_info.akey_cycle==0x31){
		led_union.led_struct.cycleled=1; 
	}else
		led_union.led_struct.cycleled=0;
}

void display_led(uint8_t *buf){
	led_number[0] = buf[0];
	led_number[1] = buf[1];
	led_number[2] = buf[2];
	led_union.led_arry[0] = buf[3];
	led_union.led_arry[1] = buf[4];
	
}

#endif